//
//  WheelView.m
//  12-转盘(了解)
//
//  Created by J on 16/3/23.
//  Copyright © 2016年 J. All rights reserved.
//

#import "WheelView.h"
#import "WheelButton.h"

@interface WheelView()
@property (weak, nonatomic) IBOutlet UIImageView *centerView;
@property (weak, nonatomic)  WheelButton *selectedButton;//记录选中button
@property(nonatomic,strong)CADisplayLink* link;


@end
@implementation WheelView
+ (instancetype)wheelView{
    
    WheelView* whell = [[[NSBundle mainBundle] loadNibNamed:@"WheelView" owner:nil options:nil]lastObject];

    return whell;
}

//        UIView 动画不能控制执行次数  所以改用定时器
//        CADisplayLink 刷新频率高 所以用这个1s 60次  并且可以暂停
          //CABasicAnimation动画的位置一切都是假象 每次点击的位置都不准确 所以改用UIView动画
- (CADisplayLink *)link{
    if (!_link) {
        CADisplayLink* link = [CADisplayLink displayLinkWithTarget:self selector:@selector(changeAngle)];
        self.link = link;
        [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    }
    
    return _link;
}


#pragma mark --每一秒旋转一定的角度  比如此处设置为45度
- (void)changeAngle{
 //每秒旋转的角度 45/60.0
    CGFloat angle  = 45/60.0/180.0*M_PI;
    _centerView.transform = CGAffineTransformRotate(_centerView.transform, angle);
}





//中间按钮开始选号
- (IBAction)picker:(UIButton *)sender {
    //中间转盘快速旋转 并且不需要与用户交互
    //开始旋转的时候link停止
        [self pause];
    
    
       //这只是一个动画效果 没有什么实际含义
        CABasicAnimation* basic = [CABasicAnimation animation];
        basic.keyPath = @"transform.rotation";
        basic.toValue = @(M_PI*2*3);//这个数值是随机的  随便转多少圈 只是一个效果而已 转弯之后 把选中的button设置指向最顶上竖直
        basic.duration = 0.5;
//        basic.repeatCount = 1;
        basic.delegate = self;
        [self.centerView.layer addAnimation:basic forKey:nil];
    
    
        //点击哪个星座就把当前星座指向中心点上面
        //根据选中按钮transform 获取按钮已经顺时针旋转了多少度 然后再通过转盘的transform 让转盘逆时针旋转回去让选中button指向最上面
        CGFloat angle = atan2(self.selectedButton.transform.b, self.selectedButton.transform.a);
    
       _centerView.transform = CGAffineTransformMakeRotation(-angle);//直接逆时针旋转回去 选中按钮的角度 让选中按钮直接指向最上面
    

    
}


#pragma mark --CABasicAnimationDelegate
//动画执行完毕
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    //动画执行完毕之后延迟一下再继续执行
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self start];
    });

    
}



- (void)start{//开始动画
    self.link.paused = NO;
}

- (void)pause{//暂停动画
    self.link.paused = YES;
    
}






- (void)awakeFromNib{//创建一堆button
    CGFloat w = self.bounds.size.width;//正方形 宽高相同

    //加载需要剪裁的大图
    UIImage* bigImageNormal = [UIImage imageNamed:@"LuckyAstrology"];//非选中
    UIImage* bigImagePress = [UIImage imageNamed:@"LuckyAstrologyPressed"];//非选中

    //像素和点的屏幕比例
    CGFloat scale =  [UIScreen mainScreen].scale;
    CGFloat  bigImageW = bigImageNormal.size.width/12.0 * scale;//裁剪是以像素为单位 所以*2 或者*3  屏幕的像素比例
    CGFloat  bigImageH = bigImageNormal.size.height * scale;

    
    //添加按钮
    for (NSUInteger i =0; i<12; i++) {
        
        WheelButton* button = [WheelButton buttonWithType:UIButtonTypeCustom];
        button.layer.anchorPoint = CGPointMake(0.5, 1);
        button.layer.position = CGPointMake(w*0.5, w*0.5);//让position指向的anchorPoint在最大的圆圈宽度和高度一半的位置

        
        //按照顺序旋转
        CGFloat angle = M_PI*2/12.0*i;//旋转角度
        button.transform = CGAffineTransformMakeRotation(angle);
        
        
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_centerView addSubview:button];
    
        
        
        if (!i) {//默认选中第0个
            self.selectedButton = button;
            button.selected = YES;
//          button.backgroundColor = [UIColor purpleColor];
        }
        
        
        
        
        //剪切图片
        
        ///  裁剪非选中状态下图片
        CGImageRef image =  CGImageCreateWithImageInRect(bigImageNormal.CGImage, CGRectMake(bigImageW*i,0 , bigImageW, bigImageH));//裁剪图片
        
        [button setImage:[UIImage imageWithCGImage:image] forState:UIControlStateNormal];
        
        ///  裁剪选中状态下图片
        image =  CGImageCreateWithImageInRect(bigImagePress.CGImage, CGRectMake(bigImageW*i,0 , bigImageW, bigImageH));//裁剪图片
        [button setImage:[UIImage imageWithCGImage:image] forState:UIControlStateSelected];
        
        
        
        //设置背景图
        UIImage* bgImage = [UIImage imageNamed:@"LuckyRototeSelected"];//设置选中状态的背景图 并且把按钮大小设置为图片大小
        [button setBackgroundImage:bgImage forState:UIControlStateSelected];
        button.bounds = CGRectMake(0, 0, bgImage.size.width, bgImage.size.height);
        
//        NSLog(@"%@",NSStringFromCGRect(button.frame));
        
        
    }
    

}

- (void)btnClick:(WheelButton*)button{
    self.selectedButton.selected = NO;//取消上次的选中
    self.selectedButton = button;//记录选中
    self.selectedButton.selected = YES;//选中当前按钮
    
}


//这个方法只是加载xib的时候调用  但是并没有连好线(连线的属性没有值) 所有属性的值是空的
//- (instancetype)initWithCoder:(NSCoder *)aDecoder{
//    if (self = [super initWithCoder:aDecoder]) {
//
////        NSLog(@"%@",_centerView);
//    }
//
//    return self;
//}

@end
